<script setup lang="ts">
import { shallowRef, ref, onBeforeMount } from 'vue';
import { VueDraggableNext } from 'vue-draggable-next';
import { useLowCodeStore } from './store/useLowCodeStore';
import Button from './components/Button/index.vue';
import Input from './components/Input/index.vue';
import ButtonSet from './components/Button/set.vue';
import InputSet from './components/Input/set.vue';

const store = useLowCodeStore();

const componentsList = {
  button: Button,
  input: Input,
  'button-set': ButtonSet,
  'input-set': InputSet,
};

const onEnd = (obj: any) => {
  const { oldDraggableIndex } = obj;
  store.previewData(store.component[oldDraggableIndex]);
  store.nowComponentsData(store.component[oldDraggableIndex]);
};

const preview = () => {
  localStorage.setItem('lowcode', JSON.stringify(store.preview));
  window.open(location.href.replace('/#/', '/preview/#/'));
};
</script>

<template>
  <div class="wrapper">
    <div class="title">pc</div>
    <div class="btn" @click="preview"><button>预览确定</button></div>
  </div>
  <div class="container">
    <aside class="components">
      <div>
        组件区域
        <VueDraggableNext
          v-model="store.component"
          :sort="false"
          :group="{
            name: 'components', //组名为 components
            pull: 'clone', //是否允许拖出当前组
            put: false, //是否允许拖入当前组
          }"
          @end="onEnd"
        >
          <div v-for="item in store.component" :key="item.id" class="tem_btn">
            {{ item.name }}
          </div>
        </VueDraggableNext>
      </div>
    </aside>
    <main class="main-content">
      <div class="content">
        <div>
          <VueDraggableNext
            v-model="store.preview"
            :group="{
              name: 'template',
              pull: false,
              put: true,
            }"
            ghost-class="ghost"
            class="canvas"
          >
            <template v-for="item in store.preview" :key="item.id">
              <component
                :is="componentsList[item?.code]"
                :data="item"
              ></component>
            </template>
          </VueDraggableNext>
        </div>
      </div>
    </main>
    <aside class="operation">
      <div>
        操作区域
        <component :is="componentsList[store?.nowComponent?.set]"></component>
      </div>
    </aside>
  </div>
</template>

<style scoped lang="scss">
body {
  margin: 0;
  padding: 0;
  border: 1px solid red;
}

.wrapper {
  height: 30px;
  display: flex;
  align-items: center;
  //border-bottom: 2px solid red;
  box-shadow: 0 2px rgba(0, 0, 0, 0.2);
  z-index: 3;
  .title {
    text-align: center;
    width: calc(100% - 80px);
  }
}

.container {
  width: 100%;
  height: calc(100% - 30px);
  display: flex;
  flex: 1;
  z-index: 2;
}

.components {
  width: 200px;

  .tem_btn {
    padding: 0px 10px;
    height: 30px;
    line-height: 30px;
    text-align: center;
    font-size: 14px;
    color: #666;
    background-color: #f2f2f2;
    border-radius: 4px;
    cursor: move;
    user-select: none;
    margin-top: 20px;
    float: left;
    margin-left: 20px;
  }
}

.operation {
  width: 200px;
}

.main-content {
  width: 100%;
  height: 100%;
  background-color: #f2f2f2;
  display: flex;

  .content {
    height: 100%;
    flex: 1;
    background-color: #f2f2f2;
  }

  .canvas {
    background-color: #fff;
    width: 100%;
    height: 100%;
  }
  .ghost {
    background-color: #f00 !important;
  }
}

@media (min-width: 1024px) {
  .wrapper {
  }
}
</style>
